package spynx.desl.runtime.operators.impl;

import java.lang.reflect.Array;
import java.util.List;

import spynx.desl.runtime.IExecutionContext;
import spynx.desl.runtime.IOperator;
import spynx.desl.runtime.IRuntimeEntity;

public class IndexOperator implements IOperator {

	@Override
	public String getOperator() {
		return "[";
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Object apply(IExecutionContext context, String operator,
			Object... args) {
		Object list = context.resolveValue(args[0]);
		int idx = (Integer) context.resolveValue(args[1]);
		if (list instanceof Array)
			return Array.get(list, idx);
		if (list instanceof List)
			return ((List) list).get(idx);
		if (list instanceof IRuntimeEntity)
			return ((IRuntimeEntity) list).applyOperator(context, operator, idx);
		return null;
	}

}
